//
//  FTPhotoCropViewController.m
//  Fetion
//
//  Created by jiazhenxiu on 14-5-8.
//  Copyright (c) 2014年 chinasofti. All rights reserved.
//

#import "FTPhotoCropViewController.h"
#import "FTPhotoCropCoverView.h"
#import "UIImageCircularBead.h"
#import "ImageUtility.h"

@interface FTPhotoCropViewController ()

@property (strong, nonatomic)UIScrollView *baseScrollView;
@property (strong, nonatomic)UIImageView *baseImageView;
@property (strong, nonatomic)FTPhotoCropCoverView *coverView;
@property (assign, nonatomic)CGFloat defaultZoom;

@end



@implementation FTPhotoCropViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

-(void)dealloc
{
    self.baseImageView = nil;
    self.baseScrollView.delegate = nil;
    self.baseScrollView = nil;
    self.coverView = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.strNavTitle = FTLocalizedString(@"Album_CropPic");
    //[self setRightButtonWithName:FTLocalizedString(@"Album_UserPhoto")];
    
    self.view.backgroundColor = [UIColor blackColor];
    self.view.userInteractionEnabled = YES;
    
    self.baseScrollView = [[UIScrollView alloc]initWithFrame:[self getBaseScrollViewRect]];
    self.baseScrollView.delegate = self;
    self.baseScrollView.clipsToBounds = NO;
    self.baseScrollView.userInteractionEnabled = YES;
    self.baseScrollView.showsHorizontalScrollIndicator = NO;
    self.baseScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.baseScrollView];
    
    self.baseImageView = [[UIImageView alloc]initWithImage:self.baseImage];
    [self.baseScrollView addSubview:self.baseImageView];
    
    self.coverView = [[FTPhotoCropCoverView alloc]initWithFrame:self.view.bounds andSelectStyle:ZSelectImage_RectStyle];
    [self.view addSubview:self.coverView];
    
    self.defaultZoom = [self getZoomRate];
    self.baseScrollView.minimumZoomScale = self.defaultZoom;
    self.baseScrollView.maximumZoomScale = self.defaultZoom*2;
    self.baseScrollView.zoomScale = self.defaultZoom;
    self.baseScrollView.contentOffset = [self getScrollPoint];
    [self setRightText:@"完成"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)leftButtonPressed:(id)sender
{
    self.baseScrollView.contentOffset = CGPointZero;
    [super leftButtonPressed:sender];
}

-(void)rightButtonPressed:(id)sender
{
    //    [self.baseScrollView setUserInteractionEnabled:NO];
    if([self.imageDelegate respondsToSelector:@selector(cropImageFinish:)])
    {
        UIImage *cropImage = [self cropImage];
        [self.imageDelegate cropImageFinish:cropImage];
    }
        self.baseScrollView.contentOffset = CGPointZero;
}

//计算scrollView默认区域
-(CGRect)getBaseScrollViewRect
{
    CGFloat x = (self.view.bounds.size.width - CropHeadImageWidth)/2;
    CGFloat y = (self.view.bounds.size.height - CropHeadImageWidth - 64)/2;
    CGRect rect = CGRectMake(x, y, CropHeadImageWidth, CropHeadImageWidth);
    
    return rect;
}

//计算首次缩放比例
-(CGFloat)getZoomRate
{
    if (self.baseImage.size.width > self.baseImage.size.height) {
        return CropHeadImageWidth/self.baseImage.size.height;
    }
    else{
        return CropHeadImageWidth/self.baseImage.size.width;
    }
}

//计算scrollView默认滚动位置
-(CGPoint)getScrollPoint
{
    if (self.baseImage.size.width > self.baseImage.size.height) {
        CGFloat x = (self.baseScrollView.contentSize.width-CropHeadImageWidth)/2;
        return CGPointMake(x, 0);
    }
    else if (self.baseImage.size.width < self.baseImage.size.height){
        CGFloat y = (self.baseScrollView.contentSize.height-CropHeadImageWidth)/2;
        return CGPointMake(0, y);
    }
    else{
        return CGPointMake(0, 0);
    }
}

//scrollView缩放代理函数
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.baseImageView;
}

//图片裁剪函数
-(UIImage*)cropImage
{
    //截取头像图片
    self.coverView.hidden = YES;
    return [ImageUtility createImageFromView:self.view frame:self.baseScrollView.frame];
    
//    CGSize imageSize = self.baseImage.size;
//    CGFloat cropWidth = imageSize.width * (CropHeadImageWidth/self.baseScrollView.contentSize.width);
//    CGFloat cropHeight = imageSize.height * (CropHeadImageWidth/self.baseScrollView.contentSize.height);
//    CGFloat x = imageSize.width * (self.baseScrollView.contentOffset.x/self.baseScrollView.contentSize.width);
//    CGFloat y = imageSize.height * (self.baseScrollView.contentOffset.y/self.baseScrollView.contentSize.height);
//    CGRect rect = CGRectMake(x, y, cropWidth, cropHeight);
//
//    CGImageRef imageRef = CGImageCreateWithImageInRect(self.baseImage.CGImage, rect);
//    UIImage *testImage = [UIImage imageWithCGImage:imageRef];
//    CGImageRelease(imageRef);
//
//    return testImage;
}
@end
